﻿using Business;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BVVD_Website.ql_thi_sinh
{
    public partial class frmKythi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvKythi.Culture = new CultureInfo("vi-VN");
                grvKythi.Rebind();

                fillgrvKythi();
                grvKythi.DataBind();

            }
        }

        protected void grvKythi_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            fillgrvKythi();

        }

        protected void grvKythi_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;                

                RadioButtonList rblTRANGTHAI = editFormItem.FindControl("rblTRANGTHAI") as RadioButtonList;
                RadTextBox txtMAKT = editFormItem.FindControl("txtMAKT") as RadTextBox;
                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {
                    txtMAKT.Enabled = true;
                }
                else
                {
                    txtMAKT.Enabled = false;
                    Label lblTRANGTHAI = editFormItem["TRANGTHAI"].FindControl("lblTRANGTHAI") as Label;
                    rblTRANGTHAI.SelectedValue = lblTRANGTHAI.Text.Equals("ACTIVE") ? "1" : "0";

                }
                
            }
        }

        protected void grvKythi_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            LST_KYTHI kt = (from dm in db.LST_KYTHIs
                                   where dm.RECID.ToString().Equals(recid)
                                   select dm).SingleOrDefault();
            string delKT = "Xóa thành công kỳ thi có mã  <font color='red'> " + kt.TENKT + "</font>";
            db.LST_KYTHIs.DeleteOnSubmit(kt);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = delKT;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br /> Nguyên nhân : " + ex.Message;
            }

            rnAlert.Show();
        }

        protected void grvKythi_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;

            LST_KYTHI kt = new LST_KYTHI
            {
                MAKT = (insertedItem.FindControl("txtMAKT") as RadTextBox).Text,
                TENKT = (insertedItem.FindControl("txtTENKT") as RadTextBox).Text,
                PT_SOTS = Convert.ToInt32((insertedItem.FindControl("txtPT_SOTS") as RadNumericTextBox).Value),
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue
            };
            BVVDDataContext db = new BVVDDataContext();
            db.LST_KYTHIs.InsertOnSubmit(kt);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Tạo mới kỳ thi <font color='red'> " + kt.TENKT + "</font> thành công ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n <br /> Nguyên nhân : " + ex.Message;

            }


            rnAlert.Show();
        }

        protected void grvKythi_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string recid = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["RECID"].ToString();

            LST_KYTHI kt = (from item in db.LST_KYTHIs
                                   where item.RECID.ToString().Equals(recid)
                                   select item).SingleOrDefault();

            kt.PT_SOTS = Convert.ToInt32((editedItem.FindControl("txtPT_SOTS") as RadNumericTextBox).Value);
            kt.TENKT = (editedItem.FindControl("txtTENKT") as RadTextBox).Text;
            kt.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            kt.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công kỳ thi có mã  <font color='red'> " + kt.TENKT +"</font>";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br /> Nguyên nhân : " + ex.Message;
            }

            rnAlert.Show();
        }

        private void fillgrvKythi()
        {

            BVVDDataContext db = new BVVDDataContext();
            List<LST_KYTHI> list = (from kt in db.LST_KYTHIs
                                    select kt).ToList();
            grvKythi.DataSource = list;

        }
    }
}